﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using HR.ProjectX.DAL.SessionStorage;
using HR.ProjectX.DAL.UnitOfWork;
using NHibernate.Criterion;
using HR.ProjectX.Models.Entities.Bookings;

namespace HR.ProjectX.DAL.Repositories
{
    public class BookingRepository : Repository<Booking, int>, IBookingRepository
    {
        public BookingRepository(IUnitOfWork uow)
            : base(uow)
        {
        }

        public BookingStatus FindBookingStatusByDescription(string description)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                    .CreateCriteria(typeof(BookingStatus))
                    .Add(Restrictions.Eq("BookingStatusDescription", description));

            return (BookingStatus)criteriaQuery.List<BookingStatus>().FirstOrDefault();
        }

        public bool IsBooked(string model, DateTime date1, DateTime date2)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                    .CreateCriteria(typeof(Booking))
                    .CreateAlias("Vehicle", "Vehicle")
                    .CreateAlias("Vehicle.Model", "Model")
                    .Add(Restrictions.Or(
                    Restrictions.Between("DateFrom", date1, date2),
                    Restrictions.Between("DateTo", date1, date2)))
                    .Add(Restrictions.Eq("Model.ModelName", model));

            IList<Booking> booking = (List<Booking>)criteriaQuery.List<Booking>();

            if (booking.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
